#!/bin/bash
# @author 贰拾壹
# https://github.com/er10yi
source /root/MagiCude/util.sh

jarStatus(){
    logInfo "检查 MagiCude 服务状态"
    jarNameArrays=("eurekaapp" "centerapp" "agentapp")
    for jarName in ${jarNameArrays[@]} ; do
        existFlag=`ps -ef|grep $jarName|grep -v grep|wc -L`
        if [ $existFlag -eq 0 ] ;then
            logErrorNotExit "$jarName 未启动，请手动执行 magicude start"
        else logInfo "$jarName 已启动"
        fi
    done
}

dockerStatus()
{
    logInfo "检查 docker 容器状态"
    dockerNameArrays=("nginxApp" "magicude_mysql" "magicude_redis" "magicude_rabbitmq")
    for imageName in ${dockerNameArrays[@]} ; do
        existFlag=`docker ps | grep $imageName |wc -L`
        if [ $existFlag -eq 0 ] ;then
            logErrorNotExit "$imageName 未启动，请手动执行 magicude start"
        else logInfo "$imageName 已启动"
        fi
    done
}

dockerStatusAndStart()
{
    systemctl start docker
    logInfo "检查 docker 容器状态并启动容器"
    dockerNameArrays=("nginxApp" "magicude_mysql" "magicude_redis" "magicude_rabbitmq")
    for imageName in ${dockerNameArrays[@]} ; do
        existFlag=`docker ps | grep $imageName |wc -L`
        if [ $existFlag -eq 0 ] ;then
            logWarn "$imageName 未启动, 正在启动 $imageName"
            docker start $imageName >/dev/null 2>&1
            logInfo "$imageName 启动完成"
        else logInfo "$imageName 已启动"
        fi
    done
}

statusAndStart()
{
    setenforce 0
    logInfo "检查 MagiCude 服务状态并启动 jar"
    jarNameArrays=("eurekaapp" "centerapp" "agentapp")
    for jarName in ${jarNameArrays[@]} ; do
        existFlag=`ps -ef|grep $jarName|grep -v grep|wc -L`
        if [ $existFlag -eq 0 ] ;then
            logWarn "$jarName 未启动，正在启动 $jarName"
            temp=$jarName
            nohup java -jar "$jarName.jar" --spring.config.location="${temp/app/}".yml  > /dev/null 2>&1 &
            logInfo "$jarName 启动完成"
        else logInfo "$jarName 已启动"
        fi
    done
}


status()
{
    dockerStatus
    jarStatus
    exit 0;
}

start()
{
    setenforce 0
    dockerStatusAndStart
    statusAndStart
    exit 0;
}

stop()
{
    logWarn "停止 MagiCude 所有服务"
    jarNameArrays=("eurekaapp" "centerapp" "agentapp")
    for jarName in ${jarNameArrays[@]} ; do
        tempPid=`ps -ef|grep $jarName|grep -v grep|cut -c 9-15`
        if [ $tempPid ] ;then
            kill -9 $tempPid
        fi
    done
    logInfo "完成"
    logWarn "停止 nmap和masscan"
    existFlag=`ps -ef|grep nmap|grep -v grep|cut -c 9-15`
    if [ $existFlag ] ;then
        kill -9 $(pidof nmap)
    fi
    existFlag=`ps -ef|grep masscan|grep -v grep|cut -c 9-15`
    if [ $existFlag ] ;then
        kill -9 $(pidof masscan)
    fi
    logInfo "完成"
    logWarn "停止 docker 容器"
    dockerNameArrays=("nginxApp" "magicude_mysql" "magicude_redis" "magicude_rabbitmq")
    for imageName in ${dockerNameArrays[@]} ; do
        existFlag=`docker ps | grep $imageName |wc -L`
        if [ $existFlag -ne 0 ] ;then
            docker stop $imageName >/dev/null 2>&1
        fi
    done
    logInfo "完成"
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    *)
    echo "魔方-MagiCude操作脚本"
    echo "@author 贰拾壹"
    echo "https://github.com/er10yi"
    echo
    echo "usage: $0 start|stop|restart|status"
    exit 0;
esac